package XishiBackend.services.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import XishiBackend.base.Constant;
import XishiBackend.models.entity.Dishes;
import XishiBackend.models.entity.Order;
import XishiBackend.models.entity.User;
import XishiBackend.models.repository.DishesRepository;
import XishiBackend.models.repository.OrderRepository;
import XishiBackend.models.repository.RestaurantRepository;
import XishiBackend.models.repository.UserRepository;

@Service
@Transactional
public class WebOrderService {
    @Autowired
    private OrderRepository oR;
    @Autowired
    private RestaurantRepository rR;
    @Autowired
    private UserRepository uR;
    @Autowired
    private DishesRepository dR;
    
    public Order findOne(long orderId) {
        return oR.findOne(orderId);
    }
    
    public List<Order> findAll() {
        Iterator<Order> iter = oR.findAll().iterator();
        List<Order> list = new ArrayList<Order>();
        while(iter.hasNext()) {
            Order o = iter.next();
            list.add(o);
        }
        return list;
    }
    
    public User findUserById(long userId) {
        return uR.findOne(userId);
    }
    
    public boolean delete(long orderId) {
        if(oR.exists(orderId)) {
            oR.delete(orderId);
            return true;
        }
        return false;
    }
    
    public boolean save(Order o) {
        Order ro = oR.save(o);
        if(!ro.getDishes().equals(o.getDishes())) {
            return false;
        }
        if(ro.getTotalPrice() != o.getTotalPrice()) {
            return false;
        }
        if(ro.getVoucherId() != o.getVoucherId()) {
            return false;
        }
        if(!ro.getRemarks().equals(o.getRemarks())) {
            return false;
        }
        if(ro.getRestaurantId() != o.getRestaurantId()) {
            return false;
        }
        if(ro.getDistributionId() != o.getDistributionId()) {
            return false;
        }
        if(ro.getStatus() != o.getStatus()) {
            return false;
        }
        if(ro.getUserId() != o.getUserId()) {
            return false;
        }
        if(ro.getOrderType() != o.getOrderType()) {
            return false;
        }
        if(ro.isHasPaid() != o.isHasPaid()) {
            return false;
        }
        return true;
    }
    
    public Map<String, Long> statistics(String startTime,String endTime) {
        List<Order> list = oR.findByStatusAndUpdateTimeBetween(Constant.COMPLETE, startTime, endTime);
        //将json中的菜品统计出来
        Map<String, Long> s = new HashMap<String, Long>();
        Set<Long> ids = new HashSet<Long>();
        for(Order o:list) {
            JSONObject dishes = JSONObject.fromObject(o.getDishes());
            Iterator<String> iter = dishes.keys();
            while(iter.hasNext()) {
                String id = iter.next();
                ids.add(Long.parseLong(id));    //将菜品id转成long并存入集合，以便接下来处理
                
                if(s.containsKey(id)) {
                    s.put(id, s.get(id) + dishes.getLong(id));
                }
                else {
                    s.put(id, dishes.getLong(id));
                }
            }
        }
        //将菜品销量更新到对应菜品表
        List<Dishes> dList = dR.findByIdInOrderBySalesDesc(ids);
        for(Dishes d:dList) {
            d.setSales(s.get(""+d.getId()));
        }
        dR.save(dList);
        return s;
    }
    
    public Map<Long,Long> getDishesTypeMap() {
        Iterator<Dishes> iter = dR.findAll().iterator();
        Map<Long, Long> r = new HashMap<Long, Long>();
        while(iter.hasNext()) {
            Dishes d = iter.next();
            r.put(d.getId(),d.getTypeId());
        }
        return r;
    }
}
